import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/king/king_scroll_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/utils/widget_utils.dart';
import '../../../utils/user.dart';
import '../../../utils/utils.dart';
import '../../../widgets/base/qkd_text.dart';

class KingScrollBar extends StatefulWidget {
  const KingScrollBar({super.key, this.info});

  final List<KingScrollEntity>? info;

  @override
  State<KingScrollBar> createState() => _KingScrollBarState();
}

class _KingScrollBarState extends State<KingScrollBar> {
  List<KingScrollEntity>?  get announces => widget.info;

  @override
  Widget build(BuildContext context) {
    return announces?.length == 0
      ? Container()
      : Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Image.asset(height: 25,Utils.getImgPath("king_scroll.png")),
            Positioned(
              left: 16,right: 16,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(child: _car())
                ],
              ),
            ),
          ],
        ),
      );
  }

  Widget _car() {
    return CarouselSlider(
      options: CarouselOptions(
        height: 25,
        scrollPhysics: const NeverScrollableScrollPhysics(),
        viewportFraction: 1,
        scrollDirection: Axis.vertical,
        enableInfiniteScroll: (announces?.length ?? 0) <= 1 ? false : true,
        autoPlay: (announces?.length ?? 0) <= 1 ? false : true,
        onScrolled: (value) {},
      ),
      items: [
        ...List.generate(announces?.length ?? 0, (i) {
          final lbt = announces?[i];
          return GestureDetector(
              onTap: () {
                if(lbt?.path.hasValue == true) {
                  User.needLogin(() {
                    Utils.doJump(lbt!.path!);
                });
                }
              },
              child: Row(
                children: [
                  Expanded(
                    child: QkdText(lbt?.msg ?? "",
                      fontSize: 12,color: Colours.white,
                      maxLines: 1,
                      textAlign: TextAlign.center
                    ),
                  )
                ],
              ));
        })
      ],
    );
  }
}
